package com.ing5.BL.android.cinece.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

import android.content.Context;

import com.ing5.BL.android.cinece.api.client.AbstractCache;

public class CacheManager extends AbstractCache {

    private Context appContext;

    public CacheManager(Context appContext){
        this.appContext = appContext;
    }

    @Override
    public String ReadCache(String fileName) {
        final File suspend_f = new File(appContext.getCacheDir(), fileName);

        String object = null;
        FileInputStream fis = null;
        ObjectInputStream is = null;

        try {

            fis = new FileInputStream(suspend_f);
            is = new ObjectInputStream(fis);
            object = (String) is.readObject();

        }catch(Exception e)
        {
            String val = e.getMessage();

        }finally {
            try {
                if (fis != null)   fis.close();
                if (is != null)   is.close();

            }
            catch (Exception e) { }
        }

        return object;
    }

    @Override
    public boolean WriteCache(String fileName, String object) {

        final File suspend_f = new File(appContext.getCacheDir(), fileName);
        
        FileOutputStream   fos  = null;
        ObjectOutputStream oos  = null;
        boolean            keep = true;

        try {
            fos = new FileOutputStream(suspend_f);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(object);
        }
        catch (Exception e) {
            keep = false;
        }
        finally {
            try {
                if (oos != null)   oos.close();
                if (fos != null)   fos.close();
                if (!keep) suspend_f.delete();
            }
            catch (Exception e) { /* do nothing */ }
        }

        return keep;
    }
    
    @Override
    public boolean SaveDictionnary(HashMap<String, String> cacheDictionnary) {
        final File dictionnaryFile = new File(appContext.getCacheDir(), "CacheDictionnary");

        FileOutputStream   fos  = null;
        ObjectOutputStream oos  = null;
        boolean keep = true;

        try {
            fos = new FileOutputStream(dictionnaryFile);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(cacheDictionnary);
        }
        catch (Exception e) {
            keep = false;
        }
        finally {
            try {
                if (oos != null)   oos.close();
                if (fos != null)   fos.close();
                if (!keep) dictionnaryFile.delete();
            }
            catch (Exception e) { /* do nothing */ }
        }

        return keep;
    }

    @Override
    public HashMap<String, String> LoadDictionnary() {
        final File suspend_f = new File(appContext.getCacheDir(), "CacheDictionnary");

        HashMap<String, String> cacheDictionnary = null;
        FileInputStream fis = null;
        ObjectInputStream is = null;


        try {

            fis = new FileInputStream(suspend_f);
            is = new ObjectInputStream(fis);
            cacheDictionnary = (HashMap<String, String>) is.readObject();

        }catch(Exception e)
        {
            String val = e.getMessage();
            cacheDictionnary = new HashMap<String, String>();
        }finally {
            try {
                if (fis != null)   fis.close();
                if (is != null)   is.close();

            }
            catch (Exception e) { }
        }

        return cacheDictionnary;
    }

    @Override
    public boolean clearCache() {
        try {
            File dir = appContext.getCacheDir();
            if (dir != null && dir.isDirectory()) {
                deleteDir(dir);
            }
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
            return dir.delete();
        }
        return false;
    }
}
